<template>
  <a-modal
    title="编辑商品分类"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="图书名称">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]" />
        </a-form-item>
        <!-- <a-form-item label="简介">
          <a-input v-decorator="['introduce', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]" />
        </a-form-item> -->
        <a-form-item label="摘要">
          <a-input v-decorator="['abstract', { rules: [{ required: true, min: 1, message: '请输入至少1个字符' }] }]" />
        </a-form-item>
        <a-form-item label="作者">
          <a-input v-decorator="['author', { rules: [{ required: true, min: 1, message: '请输入至少1个字符' }] }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as BookApi from '@/api/content/book'

export default {
  data() {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
      typeList: [
        { id: 1, type: '书院介绍' },
        { id: 2, type: '老师介绍' },
        { id: 3, type: '书院宗旨' },
        { id: 4, type: '新闻动态' },
        { id: 5, type: '招聘启示' },
      ],
      urlList: [
        { id: '/pages/index/inedx1', message: '启明1' },
        { id: '/pages/index/inedx2', message: '启明2' },
        { id: '/pages/index/inedx3', message: '启明3' },
      ],
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    edit(record) {
      // 显示窗口
      this.visible = true
      // 当前分类记录
      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },

    /**
     * 设置默认值
     */
    setFieldsValue() {
      const {
        $nextTick,
        form: { setFieldsValue },
      } = this
      $nextTick(() => {
        setFieldsValue(pick(this.record, ['name', 'abstract', 'author']))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {
        form: { validateFields },
      } = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.confirmLoading = true
      BookApi.edit({ id: this.record['id'], form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },
  },
}
</script>
